from .models import *
from .enumeration import *


def User_identity(username):
    """
    判断用户身份，普通用户返回 user，管理员返回 super
    :param username: 用户昵称
    :return:
    """
    info = User.objects.filter(username=username).first()
    if info.is_staff == 1:
        return 'super'
    else:
        return 'user'


def User_filter_username_modify_email(username, email):
    """
    修改 User 表中 该username 的邮箱为 该验证通过的邮箱
    :param username:
    :param email:
    :return:
    """
    user = User.objects.filter(username=username).first()
    user.email = email
    user.save()


def User_filter_username_email(username):
    """
    查询该用户注册时的邮箱
    :param username: 用户昵称
    :return:
    """
    info = User.objects.filter(username=username).first()
    return info.email


def User_filter_username_modify_is_active(username):
    """
    将该 username 的 is_active 设置为 0， 注意用户不存在的情况
    """
    info = User.objects.filter(username=username).first()
    if info:
        info.is_active = 0
        return 'yes'
    else:
        return '该用户不存在！'


def User_filter_username_modify_is_active_1(username, code=0):
    """
    将该 username 的 is_active 设置为 0 或 1
    """
    info = User.objects.filter(username=username).first()
    info.is_active = code
    info.save()


def User_filter_is_active(username):
    """
    查询用户的 is_active 状态
    :param username:
    :return:  is_active 的值; 如果用户不存在，返回 '该账号未被注册'
    """
    info = User.objects.filter(username=username).first()
    if info:
        if info.is_active == 1:
            return 1
        else:
            return '该账号已被冻结！'
    else:
        return '该账号未被注册!'


def User_filter_username(username):
    """
    查询该 username 是否已经注册
    :return:已经注册,返回1；否则返回0
    """
    info = User.objects.filter(username=username).first()
    if info:
        return 1
    else:
        return 0


def User_filter_info(username):
    """
    查询 权限、状态信息
    """
    info = User.objects.filter(username=username)
    return IS_STAFF[info.is_staff], IS_ACTIVE[info.is_active]


def User_modify_password(username, password):
    """
    修改用户密码
    """
    info = User.objects.filter(username=username).first()
    info.set_password(password)
    info.save()


def User_modify_is_staff(username, staff):
    """
    修改用户权限
    """
    info = User.objects.filter(username=username).first()
    info.is_staff = staff
    info.save()


def Email_Code_create(email, code):
    """
    记录存到数据库 [邮箱、验证码、时间戳]
    :param email: 接收方的邮件
    :param code: 验证码
    :return:
    """
    Email_Code.objects.create(email=email, code=code)


def Email_Code_filter_email(email):
    """
    通过email在Email_Code表里查询数据
    :param email:
    :return:
    """
    info = Email_Code.objects.filter(email=email)
    return info


def Email_Code_filter_email_order_by(email):
    """
    通过 email 在 Email_Code 表里查询数据,按最新时间排序，并取出第一条
    :param email:
    :return:
    note:[0] 与 .first() 功能相同
    """
    info = Email_Code.objects.filter(email=email).order_by('-createdate')[0]

    # info = Email_Code.objects.filter(email=email).order_by('-created').first()
    return info


def Email_Code_filter_email_delete(email):
    """
    删除 Email_Code 表中 email 匹配的数据 （可能有多条）
    @param email:
    @return:
    """
    Email_Code.objects.filter(email=email).delete()


def Detail_info_create(name, province, city, area, phone, urname, urphone, exectime, user, username):
    """
    存储用户的详细报安信息(如果已存在则为修改)
    :return:
    """
    info = Detail_info.objects.filter(userId=user).first()
    if info:
        # 更新操作
        info.name = name
        info.province = province
        info.city = city
        info.area = area
        info.phone = phone
        info.urname = urname
        info.urphone = urphone
        info.exectime = exectime
        info.userId = user
        info.username = username
        info.save()
    else:
        # 新增操作
        Detail_info.objects.create(name=name, province=province,
                                   city=city, area=area, phone=phone, urname=urname,
                                   urphone=urphone, exectime=exectime, userId=user,
                                   username=username, days=3)  # 默认可以使用3天此系统


def Detail_info_filter_user(user):
    """
    根据 用户对象 查询用户报安详细信息
    :param user: 用户对象
    :return:
    """
    detail_info = Detail_info.objects.filter(userId=user).first()
    info = {'name': detail_info.name, 'location': detail_info.location, 'phone': detail_info.phone,
            'urName': detail_info.urName, 'URPhone': detail_info.URPhone}
    return info


def Detail_info_filter_days(username):
    """
    通过 username 查询剩余天数
    :param username: 用户昵称
    :return: 剩余天数
    """
    info = Detail_info.objects.filter(username=username).first()
    if info:
        return info.days
    else:
        # 如果尚未提交个人报安信息，days默认为零
        return 0


def Detail_info_filter_username_account(username):
    """
    通过用户 账号 查询是否存在报平安账号密码
    注意： 用户没有 报安记录 时，要进行判断
    :param username: 用户账号
    :return: 如果存在，返回 'yes', 否则返回 'no'
    """
    info = Detail_info.objects.filter(username=username).first()
    # 如果该用户已经填写 报安详细信息
    if info:
        if info.account and info.password:
            return 'yes'
        else:
            return 'no'
    # 如果该用户没有填写 报安详细信息
    else:
        return 'no'


def Detail_info_filter_username_getData(username):
    """
    通过用户 账号 查询 用户报安信息
    注意：用户没有 报安记录 时，要进行判断
    :param username: 用户账号
    :return: 用户报安详细信息
    """
    info = Detail_info.objects.filter(username=username).first()
    data = {}
    # 如果该用户已经填写 报安详细信息
    if info:
        data['name'] = info.name
        data['province'] = info.province
        data['city'] = info.city
        data['area'] = info.area
        data['phone'] = info.phone
        data['urname'] = info.urname
        data['urphone'] = info.urphone
        data['exectime'] = info.exectime
    return data


def Detail_info_recharge_days(username, info):
    """
    给 username 充值 info 天数
    :param username: 用户账号
    :param info: 充值天数
    :return: 如果充值成功返回True； 如果 username 不存在，返回错误信息
    """
    Info = Detail_info.objects.filter(username=username).first()
    Info.days += info
    Info.save()


def Detail_info_add_account_password(username, account, password):
    """
    添加 username 的 account、password信息
    :param username: 昵称
    :param account: 学号
    :param password: 身份证后六位
    :return:
    """
    info = Detail_info.objects.filter(username=username).first()
    if info:
        info.account = account
        info.password = password
        info.save()
        return 'success'
    else:
        return '请先填写报平安详细信息后再绑定账号!'


def Detail_info_filter_datatime_data(datatime):
    """
    通过 时间 拿到该时间点报平安的用户信息
    :param datatime: 报平安时间点
    :return: 所有对应的用户信息
    """
    # 设置 start=1 且 天数大于 0的用户才会返回
    info = Detail_info.objects.filter(exectime=str(datatime)).filter(start=1).filter(days__gt=0)
    infolist = []
    for i in info:
        infolist.append({
            "province": i.province,
            "city": i.city,
            "district": i.area,
            "sjh": i.phone,
            "lxrxm": i.urname,
            "lxrsjh": i.urphone,
            'account': i.account,
            'password': i.password,
            'email': i.email,
            'start': i.start,
            'days': i.days,
            'username': i.username
        })
    return infolist


def Detail_info_reduce_days(users):
    """
    减少list中用户的days 一天
    :param users:
    :return:
    """
    for user in users:
        info = Detail_info.objects.filter(username=user).first()
        if info:
            info.days -= 1
            info.save()
    return 'yes'


def Detail_info_filter_username_modify_email(username, email):
    """
    修改 Detail_info 表中 该 username 的邮箱为 该验证通过的邮箱
    注意用户尚未填写 报安详细信息 的情况
    :param username:
    :param email:
    :return:
    """
    info = Detail_info.objects.filter(username=username).first()
    if info:
        info.email = email
        info.save()
        return '成功'
    # 用户尚未填写 报安详细信息 的情况
    else:
        return '您需要首先填写详细报平安信息之后再进行邮箱绑定!'


def Detail_info_filter_username_email(username):
    """
    查询该用户的邮箱是否已经绑定
    注意用户未填写详细报安的情况
    :param username:
    :return:
    """
    info = Detail_info.objects.filter(username=username).first()
    if info:
        if info.email is None or info.email == '':
            return 'no'
        else:
            return 'yes'
    # 用户未填写详细信息时，自然不会绑定邮箱
    else:
        return 'no'


def Detail_info_filter_username(username):
    """
    查询该 username 是否已经提交报安信息
    :param username:
    :return:已经提交,返回1；否则返回0
    """
    info = Detail_info.objects.filter(username=username).first()
    if info:
        return 1
    else:
        return 0


def Detail_info_filter_modify_start(username, code=0):
    """
    设置 detailinfo 的start 字段为0 或者 1
    """
    info = Detail_info.objects.filter(username=username).first()
    info.start = code
    info.save()


def Detail_info_filter_username_info(username):
    """
    查询 报安信息
    :param username:
    :return:
    """
    info = Detail_info.objects.filter(username=username).first()
    data = {'name': info.name, 'province': info.province, 'city': info.city, 'area': info.area,
            'phone': info.area, 'urname': info.urname, 'urphone': info.urphone,
            'days': info.days, 'exectime': info.exectime, 'account': info.account,
            'password': info.password, 'username': info.username, 'email': info.email}
    return data


def InvCode_filter_cdk(cdk):
    """
    查询该秘钥是否存在且有效,如果有效则将 vaild 设置为0
    注意：秘钥不存在的情况
    :param cdk: 秘钥
    :return: 如果存在且有效，返回秘钥的天数； 否则返回错误信息
    """
    info = InvCode.objects.filter(code=cdk).first()
    # 判断秘钥存在
    if info:
        # 如果秘钥有效
        if info.valid != 0:
            info.valid = 0
            info.save()
            print("----------------")
            print(f"info.capacity::{info.capacity}")
            return info.capacity
        else:
            return '该秘钥已过期!'
    else:
        return '该秘钥不存在！'


def InvCode_create(code, capacity, tips):
    """
    生成新的 邀请码 信息
    """
    InvCode.objects.create(code=code, valid=1, capacity=capacity, tips=tips)


def InvCode_filter_code(code):
    """
    查询给定 code 的信息
    :param code:
    :return:
    """
    info = InvCode.objects.filter(code=code).first()
    if info:
        data = {'code': code, 'valid': VALID[info.valid], 'capacity': info.capacity,
                'createdate': info.createdate, 'tips': info.tips}

        return data

    else:
        return '该激活码不存在!'


def InvCode_filter_code_edit(code, capacity, tips):
    """
    修改 code 的信息，注意 code 不存在的情况
    """
    info = InvCode.objects.filter(code=code).first()
    if info:
        info.capacity = capacity
        info.tips = tips
        info.save()
    else:
        return '该激活码不存在!'


def InvCode_filter_code_edit_valid(code):
    """
    修改 code 的 valid 为 0 ，注意 code 不存在的情况
    :param code:
    :return:
    """
    info = InvCode.objects.filter(code=code).first()
    if info:
        info.valid = 0
        info.save()
    else:
        return '该激活码不存在!'


def OrderList_filter_code_username(code):
    """
    根据 code 查找使用 该 code 的用户昵称， 注意 code 不存在的情况
    :param code: 秘钥
    :return:
    """
    info = OrderList.objects.filter(code=code).first()
    if info:
        data = {'code': info.code, 'username': info.username, 'capacity': info.capacity,
                'createdate': info.createdate}
        return data
    else:
        return '该邀请码不存在!'


def OrderList_create(username, cdk, info):
    """
    将用户的充值记录写入订单表
    :param username: 用户账号
    :param cdk: 充值秘钥
    :param info: 充值天数
    :return:写入成功，返回 True；否则返回 False
    """
    info = OrderList.objects.create(username=username, code=cdk, capacity=info)
    if info:
        return True
    else:
        return False
